﻿using HandyControl.Tools;
using HW.Application.Share.Events;
using HW.Application.Share.Models;
using HW.Application.Share.Prism;
using HW.Core.Ioc;
using Prism.Commands;
using Prism.Events;
using Prism.Modularity;
using Prism.Mvvm;
using Prism.Regions;
using Shell.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;

namespace Shell.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        public ICommand LoadedCommand { get; }
        public IModuleManager ModuleManager { get; }
        public IRegionManager RegionManager { get; }

        public MainWindowViewModel(IModuleManager moduleManager, IRegionManager regionManager)
        {
            ModuleManager = moduleManager;
            RegionManager = regionManager;
            LoadedCommand = new DelegateCommand(Loaded);
        }

        private void Loaded()
        {
            IRegion mainRegion = PrismProvider.RegionManager.Regions[RegionNames.MainRegion];
            mainRegion.NavigationService.Navigated += NavigationService_Navigated;

            //第一步，加载模块
            PrismProvider.ModuleManager.LoadModule(ModuleNames.ApplicationLoginModule);
            //第二步，导航区域
            PrismProvider.RegionManager.RequestNavigate(RegionNames.MainRegion, ViewNames.LoginView);

            //订阅用户登录成功事件
            PrismProvider.EventAggregator.GetEvent<LoginSuccessEvent>().Subscribe(OnLogined, ThreadOption.UIThread);



            //订阅关闭App事件
            PrismProvider.EventAggregator.GetEvent<CloseApplicationEvent>().Subscribe(()=> Application.Current.Shutdown());
            //订阅隐藏窗口事件
            PrismProvider.EventAggregator.GetEvent<HideWindowEvent>().Subscribe(()=> MainWindow.Hide());
            //订阅显示窗口事件
            PrismProvider.EventAggregator.GetEvent<ShowWindowEvent>().Subscribe(() => MainWindow.Show());
        }



        private Window MainWindow { get; set; } = Application.Current.MainWindow;
        private void NavigationService_Navigated(object sender, RegionNavigationEventArgs e)
        {
            switch (e.Uri.OriginalString)
            {
                case ViewNames.LoginView:
                    MainWindow.WindowState = WindowState.Normal;
                    MainWindow.SizeToContent = SizeToContent.WidthAndHeight;
                    MainWindow.WindowStyle = WindowStyle.None;
                    break;

                case ViewNames.MainView:
                    MainWindow.Hide();
                    MainWindow.WindowState = WindowState.Maximized;
                    MainWindow.Width = 1920;
                    MainWindow.Height = 1080;
                    MainWindow.ResizeMode = ResizeMode.NoResize;
                    MainWindow.SizeToContent = SizeToContent.Manual;
                    MainWindow.WindowState = WindowState.Maximized;
                    break;
                default:
                    break;
            }
        }

        public bool IsHardwareInitialized { get; private set; } = true;

        private void OnLogined(CurrentUser user)
        {
            if (IsHardwareInitialized)
            {
                PrismProvider.ModuleManager.LoadModule(ModuleNames.ApplicationMainModule);
                PrismProvider.RegionManager.RequestNavigate(RegionNames.MainRegion, ViewNames.MainView);
            }
            else 
            {
                PrismProvider.ModuleManager.LoadModule(ModuleNames.ApplicationInitializeModule);
                //导航到主区域
                PrismProvider.RegionManager.RequestNavigate(RegionNames.MainRegion, ViewNames.InitializeView);
            }

        }


        //#region
        //[DllImport("user32.dll")]
        //private static extern int GetWindowLong(IntPtr hWnd, int index);
        //[DllImport("user32.dll")]
        //private static extern int SetWindowLong(IntPtr hWnd, int index, int value);

        //[DllImport("user32.dll")]
        //private static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);

        //private const int GWL_EXSTYLE = -20;
        //private const int WS_EX_TRANSPARENT = 0x00000020;
        //private const int WS_EX_LAYERED = 0x00080000;

        //private const uint LWA_COLORKEY = 0x00000001;
        //private const uint LWA_ALPHA = 0x00000002;

        //private void HideWindow()
        //{
        //    var hwnd = new WindowInteropHelper(MainWindow).Handle;
        //    int style = GetWindowLong(hwnd, GWL_EXSTYLE);
        //    SetWindowLong(hwnd, GWL_EXSTYLE, style | WS_EX_TRANSPARENT | WS_EX_LAYERED);
        //    // 设置窗口透明度（可选）
        //    SetLayeredWindowAttributes(hwnd, 0, 128, LWA_ALPHA); // 128 表示半透明
        //}
        //#endregion

        public string _title = "HW上位机";
        public string Title
        {
            get { return _title; }
            set { _title = value; RaisePropertyChanged(); }
        }


    }
}
